iT邦幫忙

DAY 23
0

從0開始用Cocos2d for iPhone寫App Game的30天系列 第 23

[Day23] 遊戲Touch & Push開發 - 3

  • 分享至 

  • xImage
  •  

首先拉一個blue增加至GamePlayScene場景之中,並且設定Doc root var: _bluePlayer作為程式碼中的變數。

程式實作流程:

增加場景的點擊事件
- (void)didLoadFromCCB {
self.userInteractionEnabled = YES;
// =======================
// 程式省略...
// =======================
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
}

增加一個_bluePlayer變數,連結SpriteBuilder的藍色小人。
@interface GamePlayScene () {
// =======================
// 程式省略...
// =======================
CCSprite *_bluePlayer;
}
@end

實作畫面點擊時的_bluePlayer移動,當遊戲時間為0時就無法移動。
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (maxTimer > 0.0f) {
CCActionMoveBy *moveAction = [CCActionMoveBy actionWithDuration:0.5
position:ccp(10, 0)];
[_bluePlayer runAction:moveAction];
}
}

GamePlayScene.m 程式碼如下
#import "GamePlayScene.h"
@interface GamePlayScene () {
CGFloat maxTimer;
CCLabelTTF *_timer;
CCSprite *_bluePlayer;
}
@end
@implementation GamePlayScene
- (void)didLoadFromCCB {
self.userInteractionEnabled = YES;
maxTimer = 90.0f;
}
- (void)update:(CCTime)delta {
maxTimer -= delta;
if (maxTimer > 0.0f) {
_timer.string = [NSString stringWithFormat:@"%.0f", maxTimer];
} else {
_timer.string = @"0";
}
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (maxTimer > 0.0f) {
CCActionMoveBy *moveAction = [CCActionMoveBy actionWithDuration:0.5 position:ccp(10, 0)];
[_bluePlayer runAction:moveAction];
}
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
}
@end

結果如下:


上一篇
[Day22] 遊戲Touch & Push開發 - 2
下一篇
[Day24] 遊戲Touch & Push開發 - 4
系列文
從0開始用Cocos2d for iPhone寫App Game的30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言